package com.proj.android.canyondefense.core.impl;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

import com.proj.android.canyondefense.core.base.IAudio;
import com.proj.android.canyondefense.core.base.IMusic;
import com.proj.android.canyondefense.core.base.ISound;

public class AndroidAudio implements IAudio {
	private AssetManager _assetMgr;
	private SoundPool _soundPool;
	
	public AndroidAudio(Activity activity) {
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		this._assetMgr = activity.getAssets();
		this._soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
	}

	public IMusic newMusic(String fileName) {
		try {
			AssetFileDescriptor assetFileDescriptor = _assetMgr.openFd(fileName);
			return new AndroidMusic(assetFileDescriptor);
		} catch (IOException e) {
			throw new RuntimeException("[AndroidAudio] :: newMusic - couldn't load music '" + fileName + "'");
		}
	}

	public ISound newSound(String fileName) {
		try {
			AssetFileDescriptor assetFileDescriptor = _assetMgr.openFd(fileName);
			int sndId = _soundPool.load(assetFileDescriptor, 0);
			return new AndroidSound(_soundPool, sndId);
		} catch (IOException e) {
			throw new RuntimeException("[AndroidAudio] :: newSound - couldn't load sound '" + fileName + "'");
		}
	}

}
